package com.parleys.activity;

import java.util.List;

import roboguice.inject.InjectExtra;
import roboguice.inject.InjectView;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.google.inject.Inject;
import com.parleys.R;
import com.parleys.adapter.ParleysListAdapter;
import com.parleys.domain.Space;
import com.parleys.exception.WebserviceNotAvailableException;
import com.parleys.helper.ApplicationConstants;
import com.parleys.helper.ConnectionErrorRunnable;
import com.parleys.helper.ParleysLoadingDialog;
import com.parleys.helper.ProcessResultListRunnable;
import com.parleys.service.intf.ParleysService;
import com.parleys.service.intf.PresentationAssetsService;

/**
 * Displays a list of spaces. From this activity the user moves on to the list of channels.
 * 
 * @author David Van Droogenbroeck
 * 
 */
public class SpacesListActivity extends AbstractParleysActivity {

	@InjectExtra(ApplicationConstants.ONLINE_ARG)
	private Boolean online;

	@InjectView(R.id.list)
	protected ListView listView;

	@InjectView(R.id.emptyText)
	private TextView emptyText;

	@Inject
	private ParleysService parleysService;

	@Inject
	private PresentationAssetsService presentationAssetsService;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.parleys_list);

		if (online) {
			setTitle(getString(R.string.spaces, getString(R.string.online)));
		} else {
			setTitle(getString(R.string.spaces, getString(R.string.offline)));
		}
		emptyText.setText(getString(R.string.no_spaces_found));

		listView.setClickable(true);
		listView.setOnItemClickListener(new SpacesOnClickHandler());

		final ProgressDialog progressDialog = new ParleysLoadingDialog(this);
		progressDialog.show();

		final PresentationAssetsService presentationAssetsService = this.presentationAssetsService;
		final ParleysService parleysService = this.parleysService;

		/**
		 * Offloading the fetching of the list data to a different thread
		 */
		final Runnable fillList = new Runnable() {

			@Override
			public void run() {
				try {
					List<Space> list = parleysService.getSpaces(online);
					String assetBaseUrl = list.isEmpty() ? null : list.get(0).getAssetBaseURL();
					ParleysListAdapter parleysListAdapter = new ParleysListAdapter(SpacesListActivity.this, list,
							assetBaseUrl, presentationAssetsService);
					SpacesListActivity.this.runOnUiThread(new ProcessResultListRunnable(list, listView, emptyText,
							parleysListAdapter, progressDialog));
				} catch (WebserviceNotAvailableException e) {
					SpacesListActivity.this.runOnUiThread(new ConnectionErrorRunnable(SpacesListActivity.this,
							progressDialog));
				}
			}
		};

		new Thread(fillList).start();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem resetMenuItem = menu.add(0, 0, 0, getString(R.string.my_download_list));
		resetMenuItem.setIcon(android.R.drawable.ic_menu_save);
		return true;
	}

	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
		// goto my downloads
		if (item.getItemId() == 0) {
			startActivity(new Intent(this, MyDownloadsActivity.class));
		}
		return false;
	}

	private class SpacesOnClickHandler implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> listview, View layout, int arg2, long arg3) {
			Space parleysSpace = (Space) listview.getAdapter().getItem(arg2);
			Intent intent = new Intent(SpacesListActivity.this, ChannelListActivity.class);
			intent.putExtra(ApplicationConstants.SPACE_ARG, parleysSpace);
			intent.putExtra(ApplicationConstants.ONLINE_ARG, online);
			SpacesListActivity.this.startActivity(intent);
		}

	}

}
